//提现管理js脚本
var isRepeatCommit = false;  //防止连续点击多长提交按钮
//查询新闻
function queryNewsInfo()
{
    if(isValidQueryParam())
    {
        $("#pageNo").val(1);//查询的时候页码初始化
        var forms=$("#queryNewsInfoAction");
        var plaText=$("#platformId").find("option:selected").text(); 
        forms.attr("action","newsInfoAdminAction_queryNewsInfo.action?plateformText="+plaText);
        forms.submit();
    }
}

//设置第二次可点提交时间
function setRepeatCommitTime()
{
   isRepeatCommit = true;
   setTimeout(function(){
      isRepeatCommit = false;
   }, 3000);   //三秒后可再次提交
}
//添加新闻  add by wujun 20150511
function toAddNewsInfo()
{

    $.layer({
        type : 2,
        title : '发布消息',
        maxmin : false,
        shadeClose : false, //开启、关闭点击遮罩关闭层
        area : [ '715px', '500px' ],
        offset : [ '100px', '' ],
        iframe : {
            src : ctx_root + '/news/newsCenterAction_toAddNewsInfo.action'
        }
    });  
}

//添加新闻
function addNewsInfo()
{
	if (this.ajaxRequest_ != undefined && this.ajaxRequest_.readyState < 4) {//防止重复提交
        return;
    }
	if(!isRepeatCommit){
			setRepeatCommitTime();
		    if(isValidData())  //校验数据
		    {
		    	this.ajaxRequest_=$("#addNewsInfo").submit();
		    }
	    }
}

//编辑新闻
function editNewsInfo()
{
	if (this.ajaxRequest_ != undefined && this.ajaxRequest_.readyState < 4) {//防止重复提交
        return;
    }
    if(isValidDataEdit())  //校验数据
    {
    	this.ajaxRequest_=$("#editNewsInfo").submit();
    }
}

//校验数据是否有效
function isValidData()
{
    var isValid = true;
    var content="";
    if(CKEDITOR.instances.content != "" && CKEDITOR.instances.content !=null && CKEDITOR.instances.content!="undefined"){
    	content = CKEDITOR.instances.content.getData();
    }else{
    	content=$("#content").val();
    }
    if(isEmpty($("#title").val()))
    {
        layer.alert("标题不能为空！",8);
        isValid = false;
    }
    else if($("#title").val().length > 50 )
    {
        layer.alert("标题长度不能超过100！",8);
        isValid = false;
    }
    /*else if(isEmpty($("#summary").val()))
    {
        layer.alert("概述不能为空！",8);
        isValid = false;
    }*/
    /*else if($("#summary").val().length > 100)
    {
        layer.alert("概述长度不能超过100！",8);
        isValid = false;
    }*//*
    else if(isEmpty($("#file").val())){
    	layer.alert("图片必须上传！",8);
        isValid = false;
    }*/
    /*else if(isEmpty($("#keyWords").val()))
    {
        layer.alert("关键词不能为空！",8);
        isValid = false;
    }
    else if($("#keyWords").val().length > 50)
    {
        layer.alert("关键词长度不能超过50！",8);
        isValid = false;
    }*/
    /*else if(isEmpty($("#description").val()))
    {
        layer.alert("描述不能为空！",8);
        isValid = false;
    }
    else if($("#description").val().length > 100)
    {
        layer.alert("描述长度不能超过100！",8);
        isValid = false;
    }*/
    /*else if(isEmpty($("#type").val()))
    {
        layer.alert("请选择类型！",8);
        isValid = false;
    }*/
    else if(isEmpty(content))
    {
        layer.alert("内容不能为空！",8);
        isValid = false;
    }
    return isValid;
}

//校验数据是否有效
function isValidDataEdit()
{
    var isValid = true;
    var content="";
    if(CKEDITOR.instances.content != "" && CKEDITOR.instances.content !=null && CKEDITOR.instances.content!="undefined"){
    	content = CKEDITOR.instances.content.getData();
    }else{
    	content=$("#content").val();
    }
    if(isEmpty($("#title").val()))
    {
        layer.alert("标题不能为空！",8);
        isValid = false;
    }
    else if($("#title").val().length > 50 )
    {
        layer.alert("标题长度不能超过50！",8);
        isValid = false;
    }
    else if(isEmpty($("#summary").val()))
    {
        layer.alert("概述不能为空！",8);
        isValid = false;
    }
    else if($("#summary").val().length > 100)
    {
        layer.alert("概述长度不能超过100！",8);
        isValid = false;
    }
    else if(isEmpty($("#keyWords").val()))
    {
        layer.alert("关键词不能为空！",8);
        isValid = false;
    }
    else if($("#keyWords").val().length > 50)
    {
        layer.alert("关键词长度不能超过50！",8);
        isValid = false;
    }
    else if(isEmpty($("#description").val()))
    {
        layer.alert("描述不能为空！",8);
        isValid = false;
    }
    else if(isEmpty($("#type").val()))
    {
        layer.alert("请选择类型！",8);
        isValid = false;
    }
    else if($("#description").val().length > 100)
    {
        layer.alert("描述长度不能超过100！",8);
        isValid = false;
    }
    else if(isEmpty(content))
    {
        layer.alert("内容不能为空！",8);
        isValid = false;
    }
    return isValid;
}

//初始化错误信息
function initErrorMsg()
{
    var  operateStatus = $("#operateStatus").val();
    if(!isEmpty(operateStatus) && 1 == parseInt(operateStatus))
    {
        layer.alert("操作成功！",9,function(){
            parent.location.href="news/newsCenterAction_newsCenterPage.action";
            exit();
        });
    }
    else if(!isEmpty(operateStatus) && 0 == parseInt(operateStatus))
    {
        layer.alert("操作失败",8);
    }
    else if(!isEmpty(operateStatus) && isNaN(operateStatus))
    {
        layer.alert(operateStatus,8);
    }
}

/* 全选操作 */
function allcheckboxchange() {
    if (document.getElementById("allcheckbox").checked) {
        //console.log($('input:checkbox'));
        $('input:checkbox').each(function(index) {
            if (index != 0)
                this.checked = true;
        });
    } else {
        $('input:checkbox').each(function(index) {
            if (index != 0)
                this.checked = false;
        });
    }
}
    
/* 编辑新闻 */
function toEditNewsInfo() 
{
    var arrayObj = new Array();
    $('input:checkbox').each(function(index) {
        if (index != 0) {
            if (this.checked) {
                arrayObj.push(this.id);
            }
        }
    });
    if (arrayObj.length > 1) 
    {
        layer.alert("只能同时编辑一条数据", 8);
    } 
    else if (arrayObj.length == 0) 
    {
        layer.alert("需要选择一条数据", 8);
    } 
    else 
    {
        toEditPage(arrayObj[0]);
    }
}

//去编辑页面
function toEditPage(newsId)
{
    $.layer({
        type : 2,
        title : '编辑新闻',
        maxmin : false,
        shadeClose : false, //开启、关闭点击遮罩关闭层
        area : [ '815px', '700px' ],
        offset : [ '100px', '' ],
        iframe : {
            src : ctx_root + '/newsinfo/newsInfoAdminAction_toEditNewsInfo.action?newsId='+newsId+"&random="+Math.random()
        }
    });  
}

//获取已经选中的项
function getNewsIdArray()
{
    var arrayObj = new Array();
    $('input:checkbox').each(function(index) {
        if (index != 0) {
            if (this.checked) {
                arrayObj.push(this.id);
            }
        }
    });
    return arrayObj;
}

//发布新闻
function  publishNewsInfo(newsId)
{
    if(isEmpty(newsId))
    {
        layer.alert("请至少选择一条数据！", 8);
    }
    else
    {
        $.ajax
        ({
            url: 'newsinfo/newsInfoAdminAction_publishNewsInfo.action',
            type: 'POST',
            data : {
                "newsIds" : newsId
            },
            dataType: 'text',
            error: function()
            {
                alert('Error!');
            },
            success: function(result)
            {
                if("200" == result)
                {
                    layer.alert("发布成功！",9,function(){
                        window.location.href="newsinfo/newsInfoAdminAction_queryNewsInfo.action";
                     });
                }
                else
                {
                    layer.alert("发布失败",8);
                }
            }
        });   
    }
}

//取消发布新闻
function  cancelPublishNewsInfo(newsId)
{
    if(isEmpty(newsId))
    {
        layer.alert("请至少选择一条数据！", 8);
    }
    else
    {
        $.ajax
        ({
            url: 'newsinfo/newsInfoAdminAction_cancelPublishNewsInfo.action',
            type: 'POST',
            data : {
                "newsIds" : newsId
            },
            dataType: 'text',
            error: function()
            {
                alert('Error!');
            },
            success: function(result)
            {
                if("200" == result)
                {
                    layer.alert("取消发布成功！",9,function(){
                        window.location.href="newsinfo/newsInfoAdminAction_queryNewsInfo.action";
                     });
                }
                else
                {
                    layer.alert("取消发布失败",8);
                }
            }
        });   
    }
}

//推荐新闻
function  recommendNewsInfo(newsId)
{
    if(isEmpty(newsId))
    {
        layer.alert("请至少选择一条数据！", 8);
    }
    else
    {
        $.ajax
        ({
            url: 'newsinfo/newsInfoAdminAction_recommendNewsInfo.action',
            type: 'POST',
            data : {
                "newsIds" : newsId
            },
            dataType: 'text',
            error: function()
            {
                alert('Error!');
            },
            success: function(result)
            {
                if("200" == result)
                {
                    layer.alert("推荐成功！",9,function(){
                        window.location.href="newsinfo/newsInfoAdminAction_queryNewsInfo.action";
                     });
                }
                else
                {
                    layer.alert("推荐失败",8);
                }
            }
        });   
    }
}

//取消推荐新闻
function  cancelRecommendNewsInfo(newsId)
{
    if(isEmpty(newsId))
    {
        layer.alert("请至少选择一条数据！", 8);
    }
    else
    {
        $.ajax
        ({
            url: 'newsinfo/newsInfoAdminAction_cancelRecommendNewsInfo.action',
            type: 'POST',
            data : {
                "newsIds" : newsId
            },
            dataType: 'text',
            error: function()
            {
                alert('Error!');
            },
            success: function(result)
            {
                if("200" == result)
                {
                    layer.alert("取消推荐成功！",9,function(){
                        window.location.href="newsinfo/newsInfoAdminAction_queryNewsInfo.action";
                     });
                }
                else
                {
                    layer.alert("取消推荐失败",8);
                }
            }
        });   
    }
}

//删除新闻
function deleteNewsInfo()
{
	var arrayObj = getNewsIdArray();
    if(isEmpty(arrayObj) || arrayObj.length == 0)
    {
        layer.alert("请至少选择一条数据！", 8);
    }
    else
    {
        $.ajax
        ({
            url: 'newsinfo/newsInfoAdminAction_deleteNewsInfo.action',
            type: 'POST',
            data : {
                "newsIds" : arrayObj.toString()
            },
            dataType: 'text',
            error: function()
            {
                alert('Error!');
            },
            success: function(result)
            {
                if("deleteSuccess" == result)
                {
                    layer.alert("删除成功！",9,function(){
                        window.location.href="newsinfo/newsInfoAdminAction_queryNewsInfo.action";
                     });
                }
                else
                {
                    layer.alert("删除失败",8);
                }
            }
        });   
    }
}

/**
 * 预览
 */
	function viewArticle(){
	  	var content = CKEDITOR.instances.content.getData();
	  	if(!content || content == null){
	  		layer.alert("文章内容为空!",8);
	  	}else{
	  		$("#notice_title").html("<center>"+document.getElementById("title").value+"</center>");
			$("#viewTime").html("<center>"+(new Date()).format("yyyy年MM月dd日  hh时mm分ss秒")+"</center>");
			$("#notice_content").html(content);
			var img_w=$("#notice_content img").width();
			var img_h=$("#notice_content img").height();
			if(img_w>500 || img_h>400){
				$("#notice_content img").removeAttr("style"); 
		  		$("#notice_content img").css({ "height": "80%", "width": "80%" });
			}
			$("#viewTime1").html((new Date()).format("yyyy年MM月dd日"));
			$.layer({
				title : [ '预览', true ],
				area : [ '100%', '100%' ],
				dialog : {
					btns : 0,
					type : 2,
					msg : $("#view").html()
				}
			});
	  	}
	}



	function fileChange(){
		var fileInner = $("#file").val();
		fileInner=fileInner.split("\\");//这里要将 \ 转义一下
		var fileName = fileInner[fileInner.length-1];
		var reg=/^[\u4e00-\u9fa5aA-Za-z\d-._]+$/;
		if(!reg.test(fileName)){
			$("#file").val("");
			layer.alert("'图片名称不合法(只允许数字、字母、汉字、横线、下划线和点)！",8);
			return false;
		}
	}

